package game;

import game.Actors.Enemy;
import game.Config.Config;
import game.Config.EnemyInfo;
import game.Config.WaveConfig;

import java.util.ArrayList;
import java.util.Random;

/**
 * This class represents the waves of enemies in the game. 
 * You can add enemies to the wave and they will spawn accordingly to the
 * spawnInterval specified in the constructor
 * 
 * @author Sebastian Isheden and Victor Beer
 *
 */
public class Wave {
	private ArrayList<EnemyInfo> enemyList = new ArrayList<EnemyInfo>();

	private float currentTime;
	private float spawnInterval;
	private float downTime;
	Random randomizer;
	/**
	 * Creates a new wave which spawns enemies at the given spawn interval.
	 * @param spawnInterval The time between enemies spawns.
	 */
	public Wave(float spawnInterval){
		this.spawnInterval = spawnInterval;
		randomizer = new Random();
	}

	/**
	 * Adds a number of a specified enemy to the wave.
	 * @param info The info about the enemy.
	 * @param count The number of enemies.
	 */
	public void addEnemy(EnemyInfo info, int count){
		for(int i = 0; i<count;i++){
			enemyList.add(info);
		}
	}
	
	/**
	 * Updates the wave. 
	 * @param delta The time in millisecound since the last update.
	 */
	public void update(int delta){
		if(downTime < WaveConfig.DOWN_TIME){
			downTime+= (float)delta/Config.MILLISECONDS;
			return;
		}
		currentTime += (float)delta/Config.MILLISECONDS;
		if(this.currentTime>= spawnInterval){
			spawnEnemy();
			currentTime = 0;
		}
	}

	private void spawnEnemy() {
		EnemyInfo info = enemyList.get(randomizer.nextInt(enemyList.size()));
		enemyList.remove(info);
		new Enemy(info.getImage().copy(),LevelManager.getRandomPath().copy(),info.getSpeed(),info.getHealth(),info.getDeathAnimation());
		
	}
	
	/**
	 * Returns true if the wave has spawned all the enemies.
	 * @return True if the wave has spawned all the enemies.
	 */
	public boolean isFinished(){
		return enemyList.isEmpty();
	}
	
	/**
	 * @return A copy of the wave.
	 */
	public Wave copy(){
		Wave wave = new Wave(this.spawnInterval);
		for(EnemyInfo info : enemyList){
			wave.addEnemy(info, 1);
		}
		return wave;
	}
	
}
